﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;
//using System.Runtime.Serialization.Formatters.Soap;
//using System.Runtime.Serialization.Json;
using   System.Runtime.InteropServices; 

namespace TcpLabCommon
{
 
    /// <summary>
    /// 包头
    /// </summary>


    /// <summary>
    /// 
    /// </summary>


    //7620
    
    

    /// <summary>
    /// 203
    /// </summary>
     [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
    public struct ChannelConfig
    {
        public Int16 ChannelId;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 80)]
        public byte[] ChannelName;

        public float InstallationAngle;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
        public byte[] Unit;

        public Int16 SensorType;

        public float Sensitivity;

        public float OffsetValue;

        public float Linearity;

        public float ZeroOffset;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 80)]
        public byte[] Band1;
        public float ULimit1;
        public float LLimit1;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 80)]
        public byte[] Band2;
        public float ULimit2;
        public float LLimit2;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 80)]
        public byte[] Band3;
        public float ULimit3;
        public float LLimit3;

        public float Band1YellowValue;
        public float Band1RedValue;
        public Int16 Band1IsUser;

        public float Band2YellowValue;
        public float Band2RedValue;
        public Int16 Band2IsUser;

        public float Band3YellowValue;
        public float Band3RedValue;
        public Int16 Band3IsUser;


        public float HalfAmplitudeYellowValue;
        public float HalfAmplitudeRedValue;
        public Int16 HalfAmplitudeIsUser;

        public float OneAmplitudeYellowValue;
        public float OneAmplitudeRedValue;
        public Int16 OneAmplitudeIsUser;

        public float PassAmplitudeYellowValue;
        public float PassAmplitudeRedValue;
        public Int16 PassAmplitudeIsUser;

        public float TwoAmplitudeYellowValue;
        public float TwoAmplitudeRedValue;
        public Int16 TwoAmplitudeIsUser;
        
    }

     [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
    public struct CardConfig
    {
        public Int16 CardId;
        public Int16 KeyPhaseID;
    }

    /// <summary>
    /// 28
    /// </summary>
     [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
    public struct TestPointConfig
    {
        public Int16 TestPointId;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst =80)]
        public byte[] TestPointName;

        public Int16 TestPointCardId;

        public Int16 TestPointXChannelID;

        public Int16 TestPointYChannelID;
    }

    /// <summary>
    /// 2+20+4+2
    /// </summary>
     [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
    public struct SpeedConfig
    {
        public Int16 KeyPhaseID;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 80)]
        public byte[] KeyPhaseName;
        public float KeyPhaseAngle;
        public Int16 RotationDirection;
    }


    [StructLayoutAttribute(LayoutKind.Sequential,Pack=1)]
    public struct DataPacket
    {
        public Int16 Year;
        public byte Month;
        public byte Day;
        public byte Hour;
        public byte Minute;
        public byte Second;

        //毫秒
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public byte[] Millisecond;

        //预留区域
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public byte[] SeparateRegion;

        //转速1
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        //public byte[] Rpm1;
        public float Rpm1;

        //转速2
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        //public byte[] Rpm2;
        public float Rpm2;

        //转速3
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        //public byte[] Rpm3;
        public float Rpm3;

        //转速4
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        //public byte[] Rpm4;
        public float Rpm4;
        
        //峰峰值
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] PK2PK;

        //间隙电压
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] GapVoltage;

        //平方根值
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] RoomSquareValue;

        //一倍频幅值
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] OneMultiplierAmp;

        //一倍频相位
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] OneMultiplierPhase;

        //二倍频幅值
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] TWOMultiplierAmp;

        //二倍频相位
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] TWOMultiplierPhase;

        //半频幅值
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] HalfMultiplierAmp;

        //可选频段1
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] FrequencyRange1;

        //可选频段2
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] FrequencyRange2;

        //可选频段3
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] FrequencyRange3;

        //慢信号
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public float[] SlowSignal;

        //初始数据
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Initialdata[] InitialData;
    }

    public struct OldDataPacket
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public byte[] Blank1;

        public Int16 HeadRpm1;

        public Int16 HeadRpm2;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
        public byte[] Blank2;


        public Int16 Year;
        public Int16 Month;
        public Int16 Day;
        public Int16 Hour;
        public Int16 Minute;
        public Int16 Second;
        public Int16 MSecond;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
        public byte[] Blank3;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] PK2PK;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] DCVoltage;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] OneMultiplierAmp;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] OneMultiplierPhase;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] RoomSquareValue;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] Virtualvalue;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] FrequencyRange1;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] FrequencyRange2;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] FrequencyRange3;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] FrequencyRange4;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] FrequencyRange5;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int16[] FrequencyRange6;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public Int16[] SlowSignal;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public oldInitialdata[] OldInitialData;
    }

    public struct DataPacket138
    {
      

        public Int16 HeadRpm1;

        public int HeadRpm2;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
        public byte[] Blank1;

        public Int16 Year;
        public byte Month;
        public byte Day;
        public byte Hour;
        public byte Minute;
        public Int16 Second;
        public Int16 MSecond;


    }
    public struct CurrentDataPacketAndFFTData
    {
        public DataPacket CurrentdataPacket;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public ChVsAmpsAndPhs[] ChFFtAmpAndPhs;
    }

    public struct ChVsAmpsAndPhs
    {
        public Int16 CardID;

        public Int16 ChID;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public float[] Amps;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public float[] Phs;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public float[] DCs;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public float[] ACs;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public double[] ZDs;//原始数据震动值
    }

    public struct Initialdata
    {
        public UInt16 RPM;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public byteInt[] Data;
    }

    public struct oldInitialdata
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public byteInt[] Data;
    }


    public struct byteInt
    {
        public byte byte0;
        public byte byte1;
        public byte byte2;
        public byte byte3;
    }

    /// <summary>
    /// sqllite数据结构体
    /// </summary>
    [Serializable]
    public struct data
    {

        public char Edition;
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        //public UInt16[] ProcessSignalsChannelEnable;
        //[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        //public UInt16[] ProcessSignals;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public UInt16[] channelEnable;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public UInt16[] alarmFlag;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public int[] speed;

        public int power;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public Int16[] time;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] peak;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] GapVoltage;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] RootMSquare;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] frequency1Amp;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] frequency1Pha;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] frequency2Amp;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] frequency2Pha;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] frequencyHalfAmp;


        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] Opt1Spec;


        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] Opt2Spec;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] Opt3Spec;


        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public float[] SlowSignal;

        //初始数据
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public RawData[] rawData;

    }


    [Serializable]
    public struct data_test
    {

        public char Edition;

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
        public int[] speed;

        public int power;



    }

    /// <summary>
    /// sqllite数据结构体
    /// </summary>
    //[Serializable]
      [Serializable]
    public struct speeddata
    {
        public Int16 Edition;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
        public Int16[] time;
        public Int16 channelNum;
        public Int16 alarmFlag;
        public float totaldB;
        public float highdB;
        public float lowdB;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 31)]
        public float[] _1_3octavedB;
        public float velocityRMS;
        public float averageVoltage;
        public float displacementRMS;
    }
       [Serializable]
      public struct AllSpeed
      {
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
           public Int16[] Edition;
          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
          public speeddata[] rawData;
      }

    public struct RawData
    {

        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1024)]
        public UInt32[] Data;
    }


    /// <summary>
    /// 振动加速度结构体
    /// </summary>
    [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
    public struct Acceleration_AllChannel
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Int32[] channelstate;//是否有效
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public Acceleration[] Acceleration_all;

    }

    /// <summary>
    /// 振动加速度每个结构体
    /// </summary>
    public struct Acceleration
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 65536)]
        public float[] accelerationFFTAmpDB;//加速度FFT频谱
        public float totaldB;//振动加速度总值
        public float highdB;//高频段总值
        public float lowdB;//低频段总值
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 31)]
        public float[] _1_3octavedB;//倍频程
        public float velocityRMS;//均方根值
        public float displacementRMS;//位移有效值

    }






    /// <summary>
    /// 振动加速度每个结构体(不存FFT频谱数据)
    /// </summary>
    public struct Acceleration_NotSaveFFT
    {

        public float totaldB;//振动加速度总值
        public float highdB;//高频段总值
        public float lowdB;//低频段总值
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 31)]
        public float[] _1_3octavedB;//倍频程
        public float velocityRMS;//均方根值
        public float displacementRMS;//位移有效值
    }




    /// <summary>
    /// 只支持英文
    /// </summary>
    public class StringSerializeHelper:ISerialize
    {
        public byte[] Serialize<T>(T Data)
        {
            char[] chr = (Data as string).ToCharArray();
      
            byte[] bt = new byte[chr.Length];
            for (int i = 0; i < chr.Length; i++)
            {
                bt[i] = Convert.ToByte(chr[i]);
            }
            return bt;
        }

        public T DeSerialize<T>(byte[] Data)
        {
            string a = "";
            for (int i = 0; i < Data.Length; i++)
            {
                char c = Convert.ToChar(Data[i]);
                a = a + c.ToString();
            }
            object obj = a;
            return (T)obj;
        }
    }

    public class StructHelper : ISerialize
    {
        public byte[] Serialize<T>(T Data)
        {
            //得到结构体的大小
            int size = Marshal.SizeOf(Data);
            //创建byte数组
            byte[] bytes = new byte[size];
            //分配结构体大小的内存空间
            IntPtr structPtr = Marshal.AllocHGlobal(size);
            //将结构体拷到分配好的内存空间
            Marshal.StructureToPtr(Data, structPtr, false);
            //从内存空间拷到byte数组
            Marshal.Copy(structPtr, bytes, 0, size);
            //释放内存空间
            Marshal.FreeHGlobal(structPtr);
            //返回byte数组
            return bytes;
        }

        public T DeSerialize<T>(byte[] Data)
        {
            //得到结构体的大小
            int size = Marshal.SizeOf(typeof(T));
            //byte数组长度小于结构体的大小
            //if (size > Data.Length)
            //{
            //    //返回空
            //    return
            //}
            //分配结构体大小的内存空间
            IntPtr structPtr = Marshal.AllocHGlobal(size);
            //将byte数组拷到分配好的内存空间
            Marshal.Copy(Data, 0, structPtr, size);
            //将内存空间转换为目标结构体
            object obj = Marshal.PtrToStructure(structPtr, typeof(T));
            //释放内存空间
            Marshal.FreeHGlobal(structPtr);
            //返回结构体
            return (T)obj;
        }



       
    }
}
